<?php
namespace app\api_wp_v01\controller;

class RecycleAddress extends ApiWechatProgram {
	
	public function listing() {
		
		$field = array(
			'`recycle_address`.*'
		);
		$data_select = \app\common\model\RecycleAddressModel::field($field)
			->where('subscriber_id', $this->subscriber_id)
			->where('is_del', 0)
			->order('id DESC')
			->select();
			
		$data_list = array();
		foreach($data_select as $RecycleAddressModel){
			$data = array(
				'id' => $RecycleAddressModel->id,
				'liaison_name' => $RecycleAddressModel->liaison_name,
				'liaison_phone' => $RecycleAddressModel->liaison_phone,
				'address_detail' => $RecycleAddressModel->address,
			);
			$data_list[] = $data;
		};
		
		return $this->disposeJson(0, '', array(
			'address_list' => $data_list
		));
	}
	
	public function get_info() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$RecycleAddressModel = \app\common\model\RecycleAddressModel::get($parameter['id']);
		if((null === $RecycleAddressModel) || (0 !== $RecycleAddressModel->is_del)){
			return $this->disposeJson(1, '目标不存在，请刷新后重试');
		};
		
		$address = array(
			'liaison_name' => $RecycleAddressModel->liaison_name,
			'liaison_phone' => $RecycleAddressModel->liaison_phone,
			'position' => array(
				'longitude' => $RecycleAddressModel->position_lng,
				'latitude' => $RecycleAddressModel->position_lat,
			),
			'address_detail' => $RecycleAddressModel->address,
			'address_describe' => $RecycleAddressModel->address_describe,
		);
		
		return $this->disposeJson(0, '', $address);
		
	}
	
	public function create() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('liaison_name', $parameter) && is_string($parameter['liaison_name']) && (0 < strlen($parameter['liaison_name']));
		$judge_2 = array_key_exists('liaison_phone', $parameter) && is_string($parameter['liaison_phone']) && (1 === preg_match('/^[0-9]{11}$/', $parameter['liaison_phone']));
		$judge_3 = array_key_exists('position', $parameter) && is_array($parameter['position']);
		$judge_4 = array_key_exists('address', $parameter) && is_array($parameter['address']);
		$judge_5 = array_key_exists('address_detail', $parameter) && is_string($parameter['address_detail']) && (0 < strlen($parameter['address_detail']));
		$judge_6 = array_key_exists('address_describe', $parameter) && is_string($parameter['address_describe']) && (0 < strlen($parameter['address_describe']));
		if(!$judge_1 || !$judge_2 || !$judge_3 || !$judge_4 || !$judge_5 || !$judge_6){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$judge_1 = array_key_exists('latitude', $parameter['position']) && is_float($parameter['position']['latitude']) && (0 < $parameter['position']['latitude']);
		$judge_2 = array_key_exists('longitude', $parameter['position']) && is_float($parameter['position']['longitude']) && (0 < $parameter['position']['longitude']);
		if(!$judge_1 || !$judge_2){
			return $this->disposeJson(1, '位置参数校验失败');
		};
		
		$judge_1 = array_key_exists('province', $parameter['address']) && is_string($parameter['address']['province']) && (0 < strlen($parameter['address']['province']));
		$judge_2 = array_key_exists('city', $parameter['address']) && is_string($parameter['address']['city']) && (0 < strlen($parameter['address']['city']));
		$judge_3 = array_key_exists('district', $parameter['address']) && is_string($parameter['address']['district']) && (0 < strlen($parameter['address']['district']));
		if(!$judge_1 || !$judge_2 || !$judge_3){
			return $this->disposeJson(1, '地址参数校验失败');
		};
		
		// 获取地址ID
		$RegionProvinceModel = \app\common\model\RegionModel::where('grade', 1)->where('name', $parameter['address']['province'])->find();
		$RegionCityModel = \app\common\model\RegionModel::where('grade', 2)->where('name', $parameter['address']['city'])->find();
		$RegionDistrictModel = \app\common\model\RegionModel::where('grade', 3)->where('name', $parameter['address']['district'])->find();
		if((null === $RegionProvinceModel) || (null === $RegionCityModel) || (null === $RegionDistrictModel)){
			return $this->disposeJson(1, '转换地址ID失败');
		}else if(1 !== $RegionDistrictModel->is_recovery){
			return $this->disposeJson(1, '暂不支持该地区的回收业务，敬请期待');
		};
		
		// 准备写入
		$time = time();
		$RecycleAddressModel = new \app\common\model\RecycleAddressModel();
		$RecycleAddressModel->subscriber_id = $this->subscriber_id;
		$RecycleAddressModel->liaison_name = $parameter['liaison_name'];
		$RecycleAddressModel->liaison_phone = $parameter['liaison_phone'];
		$RecycleAddressModel->address = $parameter['address_detail'];
		$RecycleAddressModel->address_describe = $parameter['address_describe'];
		$RecycleAddressModel->position_lng = $parameter['position']['longitude'];
		$RecycleAddressModel->position_lat = $parameter['position']['latitude'];
		$RecycleAddressModel->region_province = $RegionProvinceModel->id;
		$RecycleAddressModel->region_city = $RegionCityModel->id;
		$RecycleAddressModel->region_district = $RegionDistrictModel->id;
		$RecycleAddressModel->create_time = $time;
		$RecycleAddressModel->is_del = 0;
		$result = $RecycleAddressModel->save();
		if(false == $result){
			return $this->disposeJson(1, '保存数据失败，请稍后重试');
		};
		
		return $this->disposeJson(0, '操作成功');
		
	}
	
	public function edit_info() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
		$judge_2 = array_key_exists('liaison_name', $parameter) && is_string($parameter['liaison_name']) && (0 < strlen($parameter['liaison_name']));
		$judge_3 = array_key_exists('liaison_phone', $parameter) && is_string($parameter['liaison_phone']) && (1 === preg_match('/^[0-9]{11}$/', $parameter['liaison_phone']));
		$judge_4 = array_key_exists('position', $parameter) && is_array($parameter['position']);
		$judge_5 = array_key_exists('address', $parameter) && (is_array($parameter['address']) || is_null($parameter['address']));
		$judge_6 = array_key_exists('address_detail', $parameter) && is_string($parameter['address_detail']) && (0 < strlen($parameter['address_detail']));
		$judge_7 = array_key_exists('address_describe', $parameter) && is_string($parameter['address_describe']) && (0 < strlen($parameter['address_describe']));
		if(!$judge_1 || !$judge_2 || !$judge_3 || !$judge_4 || !$judge_5 || !$judge_6 || !$judge_7){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$RecycleAddressModel = \app\common\model\RecycleAddressModel::get($parameter['id']);
		if((null === $RecycleAddressModel) || (0 !== $RecycleAddressModel->is_del)){
			return $this->disposeJson(1, '目标不存在，请刷新后重试');
		};
		
		$RecycleAddressModel->liaison_name = $parameter['liaison_name'];
		$RecycleAddressModel->liaison_phone = $parameter['liaison_phone'];
		$RecycleAddressModel->address_describe = $parameter['address_describe'];
		
		if(is_array($parameter['address'])){
			// 校验地址信息
			$judge_1 = array_key_exists('province', $parameter['address']) && is_string($parameter['address']['province']) && (0 < strlen($parameter['address']['province']));
			$judge_2 = array_key_exists('city', $parameter['address']) && is_string($parameter['address']['city']) && (0 < strlen($parameter['address']['city']));
			$judge_3 = array_key_exists('district', $parameter['address']) && is_string($parameter['address']['district']) && (0 < strlen($parameter['address']['district']));
			if(!$judge_1 || !$judge_2 || !$judge_3){
				return $this->disposeJson(1, '地址参数校验失败');
			};
			
			// 获取地址ID
			$RegionProvinceModel = \app\common\model\RegionModel::where('grade', 1)->where('name', $parameter['address']['province'])->find();
			$RegionCityModel = \app\common\model\RegionModel::where('grade', 2)->where('name', $parameter['address']['city'])->find();
			$RegionDistrictModel = \app\common\model\RegionModel::where('grade', 3)->where('name', $parameter['address']['district'])->find();
			if((null === $RegionProvinceModel) || (null === $RegionCityModel) || (null === $RegionDistrictModel)){
				return $this->disposeJson(1, '转换地址ID失败');
			}else if(1 !== $RegionDistrictModel->is_recovery){
				return $this->disposeJson(1, '暂不支持该地区的回收业务，敬请期待');
			};
			
			$RecycleAddressModel->region_province = $RegionProvinceModel->id;
			$RecycleAddressModel->region_city = $RegionCityModel->id;
			$RecycleAddressModel->region_district = $RegionDistrictModel->id;
			$RecycleAddressModel->address = $parameter['address_detail'];
			
			// 保存定位 校验
			$judge_1 = array_key_exists('latitude', $parameter['position']) && is_float($parameter['position']['latitude']) && (0 < $parameter['position']['latitude']);
			$judge_2 = array_key_exists('longitude', $parameter['position']) && is_float($parameter['position']['longitude']) && (0 < $parameter['position']['longitude']);
			if(!$judge_1 || !$judge_2){
				return $this->disposeJson(1, '位置参数校验失败');
			};
		
			$RecycleAddressModel->position_lng = $parameter['position']['longitude'];
			$RecycleAddressModel->position_lat = $parameter['position']['latitude'];
		};
		
		$result = $RecycleAddressModel->save();
		if(false == $result){
			return $this->disposeJson(0, '');
		};
		
		return $this->disposeJson(0, '');
		
	}
	
	public function del() {
		
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('id', $parameter) && is_integer($parameter['id']) && (0 < $parameter['id']);
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		$RecycleAddressModel = \app\common\model\RecycleAddressModel::get($parameter['id']);
		if((null === $RecycleAddressModel) || (0 !== $RecycleAddressModel->is_del)){
			return $this->disposeJson(1, '目标不存在');
		}else if($this->subscriber_id !== $RecycleAddressModel->subscriber_id){
			return $this->disposeJson(1, '该地址不属于你');
		};
		
		$RecycleAddressModel->is_del = 1;
		$result = $RecycleAddressModel->save();
		if(false == $result){
			return $this->disposeJson(1, '保存失败，请稍后重试');
		};
		
		return $this->disposeJson(0, '');
		
	}
	
}